package com.ks.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ks.dao.ProfessionDao;
import com.ks.dao.RankDao;
import com.ks.entity.Profession;
import com.ks.entity.Rank;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ks.base.BaseService;
import com.ks.dao.ExamParenttypeDao;
import com.ks.entity.ExamParenttype;

import net.sf.json.JSONObject;

@Service("examParenttypeService")
public class ExamParenttypeService<T> extends BaseService<T> {
    private final static Logger log = Logger.getLogger(ExamParenttypeService.class);

    @Autowired
    private ExamParenttypeDao<T> dao;
    @Autowired
    private ProfessionDao<T> professionDao;
    @Autowired
    private RankDao<T> rankDao;
    public ExamParenttypeDao<T> getDao() {
        return dao;
    }

    public List<JSONObject> queryJson(Object o) {
        // TODO Auto-generated method stub
        List<JSONObject> jsons = new ArrayList<JSONObject>();
        List<T> list = dao.queryList(o);
        //大类
        for (T t : list) {
            ExamParenttype entity = (ExamParenttype) t;
            JSONObject json = new JSONObject();
            json.put("text", entity.getName());
            json.put("id", entity.getId());
            json.put("code",entity.getCode());
            json.put("type", "type");
            //根据大类查找对应的专业
            Map<String, Object> promap = new HashMap<>();
            promap.put("type_id",entity.getId());
            List<T> prolist = professionDao.queryList(promap);
            List<JSONObject> projsonarry = new ArrayList<JSONObject>();
            //根据专业查级别
            for (T tp:prolist ) {
                JSONObject jsonpro = new JSONObject();
                Profession pentity = (Profession)tp;
                jsonpro.put("id",pentity.getId());
                jsonpro.put("text",pentity.getName());
                jsonpro.put("code",pentity.getCode());
                jsonpro.put("type", "profession");
                Map<String, Object> unitmap = new HashMap<>();
                unitmap.put("pro_id",pentity.getId());
                List<JSONObject> ranksonarry = new ArrayList<JSONObject>();
                List<T> ranklist = rankDao.queryList(unitmap);
                //级别
                for (T tr:ranklist ) {
                    JSONObject jsonrank = new JSONObject();
                    Rank rentity = (Rank)tr;
                    jsonrank.put("id",rentity.getId());
                    jsonrank.put("text",rentity.getName());
                    jsonrank.put("code",rentity.getCode());
                    jsonrank.put("type", "rank");
                    ranksonarry.add(jsonrank);
                }
                jsonpro.put("nodes",ranksonarry);
                projsonarry.add(jsonpro);
            }
            json.put("nodes", projsonarry);
            jsons.add(json);
        }
        return jsons;
    }

    public static JSONObject getJson() {
        return null;
    }
}
